package com.xyht.sca_s.student_manage_system.modules.clockIn.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.clockIn.entity.req.SmsClockInApplyAddReq;
import com.xyht.sca_s.student_manage_system.modules.clockIn.entity.req.SmsClockInApplyAllowReq;
import com.xyht.sca_s.student_manage_system.modules.clockIn.entity.req.SmsClockInApplyHistoryReq;
import com.xyht.sca_s.student_manage_system.modules.clockIn.entity.req.SmsClockInHistoryReq;
import com.xyht.sca_s.student_manage_system.modules.clockIn.service.SmsClockInApplyService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 补卡申请表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-08-03
 */
@RestController
@RequestMapping("/clockIn/clockInApply")
@SecretBody
public class SmsClockInApplyController {

    @Resource
    private SmsClockInApplyService smsClockInApplyService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("查看异常记录")
    @GetMapping("/getAbnormalRecords")
    public ResponseResult getAbnormalRecords(@RequestParam(value = "abnormalDate",required = false) String abnormalDate,
                                             HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsClockInApplyService.getAbnormalRecords(userId,abnormalDate);
    }

    @ApiModelProperty("查看审批流程")
    @GetMapping("/getClockInProcess")
    public ResponseResult getClockInProcess(){
        return smsClockInApplyService.getClockInProcess();
    }

    @ApiModelProperty("提交补卡申请")
    @PostMapping("/add")
    public ResponseResult addRepairClockInApply(@RequestBody SmsClockInApplyAddReq applyAddReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsClockInApplyService.addRepairClockInApply(userId,applyAddReq);
    }


    @ApiModelProperty("查看审批列表")
    @GetMapping("/list")
    public ResponseResult getRepairClockInApplyList( HttpServletRequest request,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "applyType") Integer applyType,
                                                     @RequestParam(value = "search_str",required = false)String search_str){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsClockInApplyService.getRepairClockInApplyList(userId,applyType,pageSize,pageNum,search_str);
    }

    @ApiModelProperty("查看审批详情")
    @GetMapping("/detail")
    public ResponseResult getRepairClockInApplyDetail(@RequestParam(value = "applyId") String applyId){
        return smsClockInApplyService.getRepairClockInApplyDetail(applyId);
    }

    @ApiModelProperty("审批")
    @PostMapping("/allow")
    public ResponseResult allowRepairClockInApply(HttpServletRequest request,@RequestBody SmsClockInApplyAllowReq allowReq){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsClockInApplyService.allowRepairClockInApply(userId,allowReq);
    }

    @ApiModelProperty("获取所有的申请")
    @GetMapping("/all")
    public ResponseResult getAllRepairClockInApply(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "allowStatus",required = false) Integer allowStatus,
                                                   @RequestParam(value = "applyName",required = false) String applyName,
                                                   @RequestParam(value = "clockInDate",required = false) String clockInDate){
        return smsClockInApplyService.getAllRepairClockInApply(allowStatus,applyName,pageSize,pageNum,clockInDate);
    }

    @ApiModelProperty(value = "导出申请历史记录")
    @PostMapping("/exportApplyHistory")
    public ResponseResult exportApplyHistory(@RequestBody SmsClockInApplyHistoryReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsClockInApplyService.exportApplyHistory(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty("获取所有的考勤记录")
    @GetMapping("/clockInAll")
    public ResponseResult getAllRepairClock(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "clockName",required = false) String clockName,
                                            @RequestParam(value = "clockInDate",required = false) String clockInDate,
                                            @RequestParam(value = "clockInType",required = false) Integer clockInType,
                                            @RequestParam(value = "clockInState",required = false) Integer clockInState){
        return smsClockInApplyService.getAllRepairClockIn(clockName,pageSize,pageNum,clockInDate,clockInType,clockInState);
    }

    @ApiModelProperty(value = "导出考勤历史记录")
    @PostMapping("/exportClockInHistory")
    public ResponseResult exportExamineHistoryList(@RequestBody SmsClockInHistoryReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsClockInApplyService.exportExamineHistoryList(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
}

